package HelloSeries.ui.prefs.elems.flextables.model;

import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import HelloSeries.metier.SiteFtp;

public class FlexTableFtpModel
    extends AbstractTableModel
    implements IFlexTableModel
{
    private SiteFtp newFtp;
	private String[] entetes;

    private ArrayList<SiteFtp> donnees;

    public FlexTableFtpModel( String[] entetes, ArrayList<SiteFtp> donnees )
    {
		this.entetes = entetes;
        this.donnees = donnees;
	}

	public int getColumnCount() {
		return entetes.length;
	}

	public int getRowCount() {
		return donnees.size()+1;
	}

	public Object getValueAt(int row, int col) {
		if (row < donnees.size()){
            SiteFtp s = donnees.get( row );
			switch (col){
    			case 0 : return new JTextField(s.getIp(),15);
    			case 1 : return new JTextField(s.getPort(),7);
                case 2 : return new JTextField( s.getFolder(), 15 );
                case 3 : return new JTextField( s.getLogin(), 10 );
    			case 4 : return new JPasswordField ( s.getPassword(), 10 );
                case 5:
                    return new JButton( "x" );
    			default : return null;
			}
		}
		else if (newFtp != null){
			switch (col){
			    case 0 : return new JTextField(newFtp.getIp(),15);
                case 1 : return new JTextField(newFtp.getPort(),7);
                case 2: return new JTextField( newFtp.getFolder(), 15 );
                case 3: return new JTextField( newFtp.getLogin(), 10 );
                case 4 : return new JPasswordField ( newFtp.getPassword(), 10 );
                case 5:
                    return new JButton( "+" );
    			default : return null;
			}
		}
		else {
			switch (col){
			    case 0 : return new JTextField("",15);
                case 1 : return new JTextField("",7);
                case 2: return new JTextField( "", 15 );
                case 3: return new JTextField( "", 10 );
                case 4 : return new JPasswordField ( "", 10 );
                case 5:
                    return new JButton( "+" );
    			default : return null;
			}
		}
	}

	public Class getColumnClass(int c) {
		if (getValueAt(0, c) != null){
			return getValueAt(0, c).getClass();
		}
		return Object.class;
	}


	public boolean isCellEditable(int row, int col) {
		return true;
	}

	public String getColumnName(int column) {
		return entetes[column];
	}

    public List<SiteFtp> getDonnees()
    {
		return donnees;
	}

	public void setValueAt(Object value, int row, int col) {

		if (row < donnees.size()){
            SiteFtp s = donnees.get( row );
			switch (col){
			case 0 :
                    s.setIp( (String) value );
				break;
                case 1:
                    s.setPort( (String) value );
				break;
                case 2:
                    s.setFolder( (String) value );
				break;
                case 3:
                    s.setLogin( (String) value );
				break;
                case 4:
                    s.setPassword( (String) value );
				break;
			}
		}
		else   {
            if ( newFtp == null )
            {
                newFtp = new SiteFtp();
            }
			switch (col){
                case 0:
                    newFtp.setIp( (String) value );
                    break;
                case 1:
                    newFtp.setPort( (String) value );
                    break;
                case 2:
                    newFtp.setFolder( (String) value );
                    break;
                case 3:
                    newFtp.setLogin( (String) value );
                    break;
                case 4:
                    newFtp.setPassword( (String) value );
                    break;
			}
		}
		fireTableCellUpdated(row, col);
	}

	public void addRow() {
        donnees.add( newFtp );
        fireTableRowsInserted( getRowCount(), getRowCount() );
        newFtp = null;
	}

	public void removeRow(int deleted_row) {
		donnees.remove(deleted_row);
        fireTableRowsDeleted( deleted_row, deleted_row );
	}

	public void raisePopUp(String mess){
		JOptionPane.showMessageDialog(new JOptionPane(),mess,"Erreur",JOptionPane.ERROR_MESSAGE);
	}
}
